import {
  Controller,
  Get,
  Post,
  Param,
  Query,
  BadRequestException,
  Inject,
  LoggerService,
} from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { WINSTON_MODULE_NEST_PROVIDER } from 'nest-winston';
import { User } from './user.entity';
import { UserService } from './user.service';
import { isNumber } from 'src/utils/util';
import { getUserDto } from './dto/get-user.dto';
import { query } from 'winston';

@Controller('user')
export class UserController {
  constructor(
    private userService: UserService,
    private configService: ConfigService,
    @Inject(WINSTON_MODULE_NEST_PROVIDER)
    private readonly logger: LoggerService,
  ) {}

  @Get()
  getUser(@Query() query: getUserDto): any {
    console.log(query);
    return this.userService.findAll(query);
  }

  @Post()
  async addUser(): Promise<any> {
    const user = { username: 'John', password: 'woshijohn' } as User;
    return await this.userService.create(user);
  }

  @Get('/profile')
  getUserProfile(): any {
    return this.userService.findProfile(2);
  }

  @Get('/logs')
  getUserLogs(): any {
    return this.userService.findUserLogs(2);
  }

  @Get('/logsByGroup')
  async getLogsByGroup(): Promise<any> {
    const res = await this.userService.getLogsByGroup(2);
    return res.map((o) => ({
      result: o.result,
      count: o.count,
    }));
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    if (!isNumber(id)) throw new BadRequestException('无效的id');
    return this.userService.findOne(+id);
  }
}
